In Azure, the HTTP-request is automatically transformed to a Pandas Dataframe. In this environment we have to prepare that frame.
In [2]:
import pandas as pd
def inputFrame(d):
"""
transforms a dictionary with parameters into a single line dataframe
"""
df = pd.DataFrame.from_dict(d, orient='index').T
return df
We need some test data, so we'll take some Fluksos from Open Grid
In [3]:
import os, sys, inspect
In [4]:
script_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
sys.path.append(os.path.join(script_dir, os.pardir, os.pardir))
In [7]:
from opengrid.library import config
c = config.Config()
sys.path.append(c.get('tmpo', 'folder'))
from opengrid.library.houseprint import houseprint
In [8]:
from configparser import SafeConfigParser
In [ ]:
hp = houseprint.Houseprint(gjson = c.get('houseprint','json'))
In [ ]:
#get all sensors from Houseprint
sensors = hp.get_sensors()
id_token = [{'sensorId':sensor.key, 'token':sensor.token} for sensor in sensors]
Azure Python modules are defined by two Pandas Dataframes as input (one can be optional) and a Pandas Dataframe as output. Azure then automatically transcribes the output to JSON.
In [ ]:
import requests
import io
In [ ]:
HTTP_ACCEPT = {
"json": "application/json",
"gz": "application/gzip"}
FLUKSO_CRT = """
-----BEGIN CERTIFICATE-----
MIIDfzCCAmegAwIBAgIJANYOkpI6yVcFMA0GCSqGSIb3DQEBBQUAMDMxCzAJBgNV
BAYTAkJFMQ8wDQYDVQQKEwZGbHVrc28xEzARBgNVBAMTCmZsdWtzby5uZXQwHhcN
MTAwNjAxMjE1ODAyWhcNMzUwNTI2MjE1ODAyWjAzMQswCQYDVQQGEwJCRTEPMA0G
A1UEChMGRmx1a3NvMRMwEQYDVQQDEwpmbHVrc28ubmV0MIIBIjANBgkqhkiG9w0B
AQEFAAOCAQ8AMIIBCgKCAQEA6CtNI3YrF/7Ak3etIe+XnL4HwJYki4PyaWI4S7W1
49C9W5AEbEd7ufnsaku3eVxMqOP6b5L7MFpCCGDiM1Zt32yYAcL65eCrofZw1DE0
SuWos0Z1P4y2rIUFHya8g8bUh7lUvq30IBgnnUh7Lo0eQT1XfnC/KMUnvseHI/iw
Y3HhYX+espsCPh1a0ATLlEk93XK99q/5mgojSGQxmwPj/91mOWmJOO4edEQAhK+u
t6wCNxZNnf9yyyzzLczwMytfrwBWJEJjJFTfr3JiEmHdl4dt7UiuElGLMr9dFhPV
12Bidxszov663ffUiIUmV/fkMWF1ZEWXFS0x+VJ52seChwIDAQABo4GVMIGSMB0G
A1UdDgQWBBQGMvERFrapN1lmOm9SVR8qB+uj/zBjBgNVHSMEXDBagBQGMvERFrap
N1lmOm9SVR8qB+uj/6E3pDUwMzELMAkGA1UEBhMCQkUxDzANBgNVBAoTBkZsdWtz
bzETMBEGA1UEAxMKZmx1a3NvLm5ldIIJANYOkpI6yVcFMAwGA1UdEwQFMAMBAf8w
DQYJKoZIhvcNAQEFBQADggEBAOZjgNoNhJLckVMEYZiYWqRDWeRPBkyGStCH93r3
42PpuKDyysxI1ldLTcUpUSrs1AtdSIEiEahWr6zVW4QW4o9iqO905E03aTO86L+P
j7SIBPP01M2f70pHpnz+uH1MDxsarI96qllslWfymYI7c6yUN/VciWfNWa38nK1l
MiQJuDvElNy8aN1JJtXHFUQK/I8ois1ATT1rGAiqrkDZIm4pdDmqB/zLI3qIJf8o
cKIo2x/YkVhuDmIpU/XVA13csXrXU+CLfFyNdY1a/6Dhv2B4wG6J5RGuxWmA+Igg
TTysD+aqqzs8XstqDu/aLjMzFKMaXNvDoCbdFQGVXfx0F1A=
-----END CERTIFICATE-----"""
def AzureMethod(dataframe1=None, dataframe2=None):
def getMetadata():
headers = {
"Accept": HTTP_ACCEPT["json"]
}
url = "https://api.flukso.net/sensor/{}".format(sensorId)
parameters = {
"param": "all",
"token" : token,
"version" : "1.0"
}
f = requests.get(
url,
params=parameters,
headers=headers,
verify=crt
)
print f.json()
def
#unpack parameters
sensorId = dataframe1['sensorId'][0]
token = dataframe1['token'][0]
crt = "flukso.crt"
with io.open(crt, "wb") as f:
f.write(FLUKSO_CRT.encode("ascii"))
getMetadata()
return dataframe1
In [ ]:
import datetime as dt
for sensorparam in id_token:
#add extra parameters
sensorparam['resolution'] = 'd'
sensorparam['start'] = dt.datetime.utcnow().isoformat()
#create dataframe
df = inputFrame(sensorparam)
#print result of Azure Method
AzureMethod(dataframe1 = df)
In [ ]:
In [ ]: